package net.huashitong.user.web;

import java.util.ArrayList;
import java.util.List;

import net.huashitong.user.databean.CollectionDatabean;
import net.huashitong.user.entity.Collection;
import net.huashitong.user.filter.CollectionFilter;
import net.huashitong.user.service.CollectionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
/**
 * 收藏
 * @author lzr
 *
 */
@Controller
@RequestMapping("/app/collection")
public class CollectionController extends JsonBaseController {

	@Autowired
	private CollectionService collectionService;
	@Override
	protected String getView(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 收藏功能
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/saveCollection")
	public Message saveCollection(@RequestBody CollectionFilter filter){
		if(StringUtils.isNotBlank(filter.getAlertId())&&StringUtils.isNotBlank(filter.getUserId())){
			Collection collection = collectionService.getByUserIdAndAlertId(filter);
			if(ObjectUtils.isNotBlank(collection)){
				collectionService.delete(collection);
				return coverMessage("200", "取消收藏");
			}
		}
		Collection collection = new Collection();
		collection.setAlertId(filter.getAlertId());
		collection.setPhotoId(filter.getPhotoId());
		collection.setUserId(filter.getUserId());
		collection.setText(filter.getText());
		collection.setDate(filter.getDate());
		collectionService.save(collection);
		return coverMessage("200","收藏成功");
	}
	/**
	 * 查询收藏
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryCollection")
	public Message queryCollection(@RequestBody CollectionFilter filter){
		List<Collection> collections = collectionService.queryByUserId(filter);
		List<CollectionDatabean> databeans = new ArrayList<CollectionDatabean>();
		for (Collection collection : collections) {
			CollectionDatabean databean = new CollectionDatabean();
			databean.setAlertId(collection.getAlertId());
			databean.setDate(collection.getDate());
			databean.setPhotoId(collection.getPhotoId());
			databean.setText(collection.getText());
			databeans.add(databean);
		}
		return coverMessage("200", "获取成功", databeans);
	}
}
